package ccuserinterface;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

import datamodels.Conference;
import datamodels.GlobalDataStore;
import datamodels.Manuscript;
import datamodels.User;
/**
 * A Manuscriptlistframe, this display all the manuscript for the program chair to select in order for program
 * chair to make the decision on the paper. 
 * @author rithychhen
 *
 */
@SuppressWarnings("serial")
public class ManuscriptListFrame extends JFrame {

	private Conference my_conference;
	private User my_user;
	private GlobalDataStore my_data_store;
	private String my_title;
	/**
	 * the constructor of the mansucriptlistframe.
	 * @param the_conference the current conference that is selected by the user. 
	 * @param the_user the user of the current conference.
	 * @param the_role the role of this user. 
	 * @param the_data_store the data store object use to write back to the file after the change
	 * has been made.
	 */
	public ManuscriptListFrame(Conference the_conference, User the_user, GlobalDataStore the_data_store) {
		
		my_conference = the_conference;
		my_user = the_user;
		my_data_store = the_data_store;
		setLayout(null);
		setVisible(true);
		setTitle("Make Decision: select a manuscript");
		setPreferredSize(new Dimension(500,400));
		setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		setUpFrame();
		setResizable(false);
		pack();
	}
	/**
	 * set up the frame, include the buttons and the necessary element in it. 
	 * also include the functionality of each component.
	 */
	private void setUpFrame() {
		JLabel select_man = new JLabel("Select a manuscript you want to change the decision");
		select_man.setBounds(10, 30, 400, 20);
		add(select_man);
		JPanel panel = new JPanel();
		panel.setBackground(Color.WHITE);
		panel.setLayout(new javax.swing.BoxLayout(panel, javax.swing.BoxLayout.Y_AXIS));
		JScrollPane pane = new JScrollPane(panel);
		pane.setBounds(100, 60, 375, 300);
		add(pane);
		JButton proceed = new JButton("Proceed");
		proceed.setBounds(10, 60, 80, 20);
		proceed.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				
				if (my_title != null) {
					for (int i = 0; i < my_conference.getManuscripts().size(); i++) {
					
						if (my_title.equals(my_conference.getManuscripts().get(i).getTitle())) {
							Manuscript man = my_conference.getManuscripts().get(i);
							dispose();
							new DecisionFrame(man,my_user, my_data_store);
						}
					}
				} else {
						JOptionPane.showMessageDialog(null, "Please Select a Manuscript");
					
				}
			}
			
		});
		add(proceed);
		ButtonGroup group = new ButtonGroup();
		for (int i = 0; i < my_conference.getManuscripts().size(); i++) {
			final JCheckBox box = new JCheckBox(my_conference.getManuscripts().get(i).getTitle());
			box.addActionListener(new ActionListener() {

				@Override
				public void actionPerformed(ActionEvent e) {
					if(box.isSelected()) {
						my_title = box.getText();
						
					}
					
				}
				
			});
			group.add(box);
			panel.add(box);
		}
	}
}
